//
//  main.m
//  FileOperation
//
//  Created by TTKai on 11-8-9.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // insert code here...
    NSString *fName = @"/Users/TTKai/documents/test.txt";
    NSFileManager *fm;
    NSDictionary *attr;
    
    fm = [NSFileManager defaultManager];
    
    NSLog([fm currentDirectoryPath]);
    
    NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
    
    NSLog(resourcePath);
    
    if ([fm fileExistsAtPath:fName] == NO)
    {
        NSLog(@"File doesn't exist!");
        return 1;
    }
    
    NSError * error = nil;
    NSString *fnewName = @"/Users/TTKai/documents/test2.txt";
    if ([fm copyItemAtPath:fName toPath:fnewName error:&error] == NO)
    {
        NSLog(@"file copy failed: %@!", [error localizedFailureReason]);
    }
    
    if ([fm contentsEqualAtPath:fName andPath:fnewName] == NO)
    {
        NSLog(@"file not equal");
    }
    else
    {
        NSLog(@"file queal");
    }
    
    NSString *fnewName2 = @"/Users/TTKai/documents/test3.txt";
    if ([fm moveItemAtPath:fnewName toPath:fnewName2 error:&error] == NO)
    {
        NSLog(@"file move failed: %@", [error localizedFailureReason]);
    }
    
    if ((attr = [fm attributesOfItemAtPath:fnewName2 error:&error]) == nil)
    {
        NSLog(@"Couldn't get file attributes: %@", error);
        return 5;
    }
//    NSLog(@"%@", attr);
    NSLog(@"File size is %i bytes", [[attr objectForKey:NSFileSize] intValue]);

    if ([fm removeItemAtPath:fnewName2 error:&error] == NO)
    {
        NSLog(@"File Remove failed: %@", [error localizedFailureReason]);
    }
    
//    if ([fm removeItemAtPath:fnewName error:&error] == NO)
//    {
//        NSLog(@"File Remove failed: %@", [error localizedFailureReason]);
//    }
    
    NSString *content = [NSString stringWithContentsOfFile:fName encoding:NSUTF8StringEncoding error:&error];
    NSLog(@"%@", content);
    
    NSData * fileData = [fm contentsAtPath:fName];
    if (fileData == nil)
    {
        NSLog(@"File Read failed");
        return 6;
    }
    
	NSString *fnewName3 = @"/Users/TTKai/documents/test4.txt";
    if ([fm createFileAtPath:fnewName3 contents:fileData attributes:nil] == NO)
    {
		NSLog(@"Couldn't create the copy");
		return 7;
    }
	
	[fm removeItemAtPath:fnewName3 error:nil];
	
	NSString *Dir1 = @"/Users/TTKai/documents/testDir/";
	if ([fm createDirectoryAtPath:Dir1 withIntermediateDirectories:YES attributes:nil error:&error] == NO)
	{
		NSLog(@"Couldn't create directory:%@", [error localizedFailureReason]);
	}
	
	NSDirectoryEnumerator *dirEnum;
	NSArray *dirArray;
	NSString *path = @"/Users/TTKai/documents/";
	NSLog(@"\n\nContents of %@:", path);
	
	dirEnum = [fm enumeratorAtPath:path];
	
	while ((path = [dirEnum nextObject]) != nil)
	{
		NSLog(@"%@", path);
	}
	
	NSString *path2 = @"/Users/TTKai/documents/";
	NSLog(@"\n\nContents Array of %@:", path2);
	dirArray = [fm contentsOfDirectoryAtPath:path2 error:&error];
	NSLog(@"%@", dirArray);
	
	NSString *tmpDir = NSTemporaryDirectory();
	NSLog(@"TemporaryDirectory is %@", tmpDir);
	
	NSString *fileName = @"/path.mb";
	NSString *fullPath = [path2 stringByAppendingPathComponent:fileName];
	NSLog(@"fullPath is %@", fullPath);
	NSLog(@"extension is %@", [fullPath pathExtension]);
	NSLog(@"pathComponents is %@", [fullPath pathComponents]);

	NSString *uPath = @"~/a/b/bc/uPath.mm";
	NSLog(@"uPath is: %@", [uPath stringByStandardizingPath]);
	
	NSLog(@"lastPathComponent %@", [uPath lastPathComponent]);
	
	NSLog(@"HomeDir is:%@", NSHomeDirectory());
	
	NSLog(@"HomeDirForUser is:%@", NSHomeDirectoryForUser(@"wheel"));
	
	NSLog(@"userName is %@", NSUserName());
	NSLog(@"fullUserName is %@", NSFullUserName());
	
	NSProcessInfo *proc = [NSProcessInfo processInfo];
	NSLog(@"processInfo is: %@", [proc processName]);
	
//	[proc release];
	NSLog(@"retain count=%i", [proc retainCount]);
	
//	[dirArray retain];
	NSLog(@"retian count dirArray = %i", [dirArray retainCount]);
	
    
	NSNumber *number1 = [[NSNumber alloc] initWithInt:1];
	NSNumber *number2 = [[NSNumber alloc] initWithInt:2];
	NSString *str = [[NSString alloc] initWithFormat:@"123"];
	NSString *str2 = [[NSString alloc] initWithFormat:@"456"];
	
	NSArray *list = [[NSArray alloc] initWithObjects:str, nil];
	NSLog(@"retian count %d %d %d %d %d", (int)[number1 retainCount], (int)[number2 retainCount], (int)[list retainCount], (int)[str retainCount], (int)[str2 retainCount]);
//	[list ob
	[str release];
	NSLog(@"retain c %d", (int)[str retainCount]);
	NSLog(@"%@", str);
	[str release];
	NSLog(@"retain c %d", (int)[str retainCount]);
	[str release];
	NSLog(@"retain c %d", (int)[str retainCount]);
	
	
    [pool drain];
//	[dirArray release];
    return 0;
}

